﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VscCppProjCreator
{
    public partial class FormMain : Form
    {
        //class FileTool
        //{
        //    public delegate bool DelegateRead(StreamReader sr, ref string strErr);
        //    public delegate bool DelegateWrite(StreamWriter sw, ref string strErr);
        //    public static bool Read(string filename, DelegateRead read, ref string strErr)
        //    {
        //        bool r = false;
        //        strErr = "";

        //        FileStream fileStream = null;
        //        StreamReader streamReader = null;
        //        try
        //        {
        //            fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
        //            if (fileStream == null)
        //            {
        //                strErr = "读文件时，打开文件失败。";
        //                return r;
        //            }
        //            streamReader = new StreamReader(fileStream/*, Encoding.Default*/);
        //            r = read(streamReader, ref strErr);
        //        }
        //        catch/* (Exception ex)*/
        //        {
        //            strErr = "读文件时出错。";
        //            r = false;
        //        }
        //        finally
        //        {
        //            if (streamReader != null)
        //            {
        //                streamReader.Close();
        //            }
        //            if (fileStream != null)
        //            {
        //                fileStream.Close();
        //            }
        //        }

        //        return r;
        //    }

        //    public static bool Write(string filename, DelegateWrite write, ref string strErr)
        //    {
        //        return Write(filename, Encoding.UTF8, write, ref strErr);
        //    }

        //    public static bool Write(string filename, Encoding encoding, DelegateWrite write, ref string strErr)
        //    {
        //        bool r = false;
        //        strErr = "";

        //        FileStream fileStream = null;
        //        StreamWriter streamWriter = null;
        //        try
        //        {
        //            fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write);
        //            if (fileStream == null)
        //            {
        //                strErr = "写文件时，打开文件失败。";
        //                return r;
        //            }
        //            streamWriter = new StreamWriter(fileStream, encoding);
        //            r = write(streamWriter, ref strErr);
        //        }
        //        catch (Exception ex)
        //        {
        //            strErr = "写文件时出错。";
        //            r = false;
        //        }
        //        finally
        //        {
        //            if (streamWriter != null)
        //            {
        //                streamWriter.Flush();
        //                streamWriter.Close();
        //            }
        //            if (fileStream != null)
        //            {
        //                fileStream.Close();
        //            }
        //        }

        //        return r;
        //    }

        //    public static bool Append(string filename, DelegateWrite write, ref string strErr)
        //    {
        //        bool r = false;
        //        strErr = "";

        //        FileStream fileStream = null;
        //        StreamWriter streamWriter = null;

        //        try
        //        {
        //            fileStream = new FileStream(filename, FileMode.Append, FileAccess.Write);
        //            if (fileStream == null)
        //            {
        //                strErr = "文件追加记录时，打开文件失败。";
        //                return r;
        //            }
        //            streamWriter = new StreamWriter(fileStream);
        //            r = write(streamWriter, ref strErr);
        //        }
        //        catch// (Exception ex)
        //        {
        //            strErr = "文件追加记录时出错。";
        //            r = false;
        //        }
        //        finally
        //        {
        //            if (streamWriter != null)
        //            {
        //                streamWriter.Flush();
        //                streamWriter.Close();
        //            }
        //            if (fileStream != null)
        //            {
        //                fileStream.Close();
        //            }
        //        }

        //        return r;
        //    }


        //    public delegate bool DelegateReadBinary(BinaryReader br, ref string strErr);
        //    public delegate bool DelegateWriteBinary(BinaryWriter bw, ref string strErr);
        //    public static bool ReadBinary(string filename, DelegateReadBinary read, ref string strErr)
        //    {
        //        bool r = false;
        //        strErr = "";

        //        FileStream fileStream = null;
        //        BinaryReader binaryReader = null;

        //        try
        //        {
        //            fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
        //            if (fileStream == null)
        //            {
        //                strErr = "读文件时，打开文件失败。";
        //                return r;
        //            }
        //            binaryReader = new BinaryReader(fileStream);
        //            r = read(binaryReader, ref strErr);
        //        }
        //        catch //(Exception)
        //        {
        //            strErr = "读文件时出错.";
        //            r = false;
        //        }
        //        finally
        //        {
        //            if (binaryReader != null)
        //            {
        //                binaryReader.Close();
        //            }
        //            if (fileStream != null)
        //            {
        //                fileStream.Close();
        //            }
        //        }

        //        return r;
        //    }

        //    public static bool WriteBinary(string filename, DelegateWriteBinary write, ref string strErr)
        //    {
        //        bool r = false;
        //        strErr = "";

        //        FileStream fileStream = null;
        //        BinaryWriter binaryWriter = null;

        //        try
        //        {
        //            fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write);
        //            if (fileStream == null)
        //            {
        //                strErr = "写文件时，打开文件失败。";
        //                return r;
        //            }
        //            binaryWriter = new BinaryWriter(fileStream);
        //            r = write(binaryWriter, ref strErr);
        //        }
        //        catch // (Exception)
        //        {
        //            strErr = "写文件时出错。";
        //            r = false;
        //        }
        //        finally
        //        {
        //            if (binaryWriter != null)
        //            {
        //                binaryWriter.Flush();
        //                binaryWriter.Close();
        //            }
        //            if (fileStream != null)
        //            {
        //                fileStream.Close();
        //            }
        //        }

        //        return r;
        //    }

        //    public static bool AppendBinary(string filename, DelegateWriteBinary write, ref string strErr)
        //    {
        //        bool r = false;
        //        strErr = "";

        //        FileStream fileStream = null;
        //        BinaryWriter binaryWriter = null;

        //        try
        //        {

        //            fileStream = new FileStream(filename, FileMode.Append, FileAccess.Write);
        //            if (fileStream == null)
        //            {
        //                strErr = "写文件时，打开文件失败。";
        //                return r;
        //            }
        //            binaryWriter = new BinaryWriter(fileStream);
        //            r = write(binaryWriter, ref strErr);
        //        }
        //        catch// (Exception)
        //        {
        //            strErr = "文件追加记录时出错。";
        //            r = false;
        //        }
        //        finally
        //        {
        //            if (binaryWriter != null)
        //            {
        //                binaryWriter.Flush();
        //                binaryWriter.Close();
        //            }
        //            if (fileStream != null)
        //            {
        //                fileStream.Close();
        //            }
        //        }

        //        return r;
        //    }
        //}

        public FormMain()
        {
            InitializeComponent();

            this.textBox1.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            this.textBox2.Text = "MyFavoriteProj";
            //this.comboBox1.Items.Clear();
            //this.comboBox1.Items.AddRange(new object[] { "Linux", "Mac", "Win32" });
            //this.comboBox1.SelectedIndex = 0;
            this.textBox3.Text = "/usr/bin/gcc";
            this.textBox4.Text = "/usr/bin/g++";
            this.textBox5.Text = "c++11";
            this.textBox6.Text = "c11";
            this.textBox7.Text = "/usr/bin/echo";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog f = new FolderBrowserDialog();
            f.Description = "选择项目路径";
            f.ShowNewFolderButton = true;
            f.SelectedPath = string.IsNullOrWhiteSpace(this.textBox1.Text) ? Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) : this.textBox1.Text.Trim();
            DialogResult dialogResult = f.ShowDialog();
            if (dialogResult == DialogResult.OK || dialogResult == DialogResult.Yes)
            {
                this.textBox1.Text = f.SelectedPath;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string resDir = Application.StartupPath + "/res";
            string projRootDir;

            //项目路径
            Directory.CreateDirectory(this.textBox1.Text.Trim());
            //项目根目录
            Directory.CreateDirectory(projRootDir = this.textBox1.Text.Trim() + "/" + this.textBox2.Text.Trim());

            //.vscode
            string dirvscode;
            Directory.CreateDirectory(dirvscode = projRootDir + "/.vscode");
            File.WriteAllText(
                projRootDir + "/.vscode" + "/c_cpp_properties.json",
                File.ReadAllText(resDir + "/.vscode" + "/c_cpp_properties.json")
                    //.Replace("<<<PLATFORM>>>", this.comboBox1.Text.Trim())
                    .Replace("<<<COMPILERPATH>>>", this.textBox3.Text.Trim())
                    .Replace("<<<CSTANDARD>>>", this.textBox6.Text.Trim())
                    .Replace("<<<CPPSTANDARD>>>", this.textBox5.Text.Trim()));
            File.WriteAllText(
                projRootDir + "/.vscode" + "/launch.json",
                File.ReadAllText(resDir + "/.vscode" + "/launch.json")
                    .Replace("<<<PROJECTNAME>>>", this.textBox2.Text.Trim())
                    .Replace("<<<ECHOPATH>>>", this.textBox7.Text.Trim()));
            File.WriteAllText(
                projRootDir + "/.vscode" + "/tasks.json",
                File.ReadAllText(resDir + "/.vscode" + "/tasks.json"));

            //build
            Directory.CreateDirectory(projRootDir + "/build");
            Directory.CreateDirectory(projRootDir + "/build" + "/debug");
            Directory.CreateDirectory(projRootDir + "/build" + "/release");
            Directory.CreateDirectory(projRootDir + "/build" + "/relwithdebinfo");

            //3rdparty
            string dir3rdparty;
            Directory.CreateDirectory(dir3rdparty = projRootDir + "/3rdparty");

            //deploy
            string dirdeploy;
            Directory.CreateDirectory(dirdeploy = projRootDir + "/deploy");

            //doc
            string dirdoc;
            Directory.CreateDirectory(dirdoc = projRootDir + "/doc");
            File.WriteAllText(
                projRootDir + "/doc" + "/CMakeLists_template.txt",
                File.ReadAllText(resDir + "/doc" + "/CMakeLists_template.txt"));

            //src
            string dirsrc;
            Directory.CreateDirectory(dirsrc = projRootDir + "/src");
            Directory.CreateDirectory(dirsrc + "/_configuration");
            Directory.CreateDirectory(dirsrc + "/_data");
            Directory.CreateDirectory(dirsrc + "/_resources");
            File.WriteAllText(
                projRootDir + "/src" + "/AppStart.cc",
                File.ReadAllText(resDir + "/src" + "/AppStart.cc"));
            File.WriteAllText(
                projRootDir + "/src" + "/CMakeLists.txt",
                File.ReadAllText(resDir + "/src" + "/CMakeLists.txt"));

            //
            File.WriteAllText(
                projRootDir + "/CMakeConf.txt",
                File.ReadAllText(resDir + "/CMakeConf.txt"));
            File.WriteAllText(
                projRootDir + "/CMakeLists.txt",
                File.ReadAllText(resDir + "/CMakeLists.txt")
                .Replace("<<<PROJECTNAME>>>", this.textBox2.Text.Trim()));
            File.WriteAllText(
                projRootDir + "/LICENSE",
                File.ReadAllText(resDir + "/LICENSE"));
            File.WriteAllText(
                projRootDir + "/main.cc",
                File.ReadAllText(resDir + "/main.cc"));
            File.WriteAllText(
                projRootDir + "/" + this.textBox2.Text.Trim() + ".code-workspace",
                File.ReadAllText(resDir + "/PROJECTNAME.code-workspace"));
            File.WriteAllText(
                projRootDir + "/README",
                File.ReadAllText(resDir + "/README"));

            MessageBox.Show("生成成功!");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(this.textBox1.Text))
            {
                System.Diagnostics.Process.Start("explorer.exe", this.textBox1.Text);
            }
        }
    }
}
